﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.IO;
using System.Security.Cryptography;
using GemBox.Spreadsheet;

namespace HotelSoft.HotelManagement_BUS
{
    class XuatHoaDonThanhToan
    {
        public ArrayList dsYeuCauBaoCao = new ArrayList();
        public ArrayList dsCotBaoCao = new ArrayList();
        public XuatHoaDonThanhToan()
        {
            dsCotBaoCao.Add("MÃ SỐ NV");
            dsCotBaoCao.Add("MÃ HOÁ ĐƠN");
            dsCotBaoCao.Add("CMND");
            dsCotBaoCao.Add("HỌ TÊN KH");
            dsCotBaoCao.Add("SỐ PHÒNG");
            dsCotBaoCao.Add("TỔNG TIỀN");
            dsCotBaoCao.Add("NGÀY THANH TOÁN");
            dsCotBaoCao.Add("NHÂN VIÊN LẬP HĐ");
            dsCotBaoCao.Add("SỐ PHIẾU ĐK");
        }

        public static bool SoSanhChuoi(String a, String b)
        {
            String a1 = a.ToLower();
            String b1 = b.ToLower();
            a1 = a1.Trim();
            b1 = b1.Trim();
            if (a1.Contains(b1))
                return true;
            return false;
        }

        public static void ExportToExcel(DataTable kqBaoCao, string filename)
        {
            ExcelFile excelFile = new ExcelFile();
            ExcelWorksheet ws = excelFile.Worksheets.Add("Danh sach");

            int iRows = kqBaoCao.Rows.Count;
            int iColumns = kqBaoCao.Columns.Count;
            XuatHoaDonThanhToan XuatHoaDonThanhToan = new XuatHoaDonThanhToan();
            for (int iTenCot = 0; iTenCot < iColumns; iTenCot++)
            {
                ws.Cells[0, iTenCot].Value = XuatHoaDonThanhToan.dsCotBaoCao[iTenCot].ToString().ToUpper();
                ws.DefaultColumnWidth = 4000;
            }
            for (int i = 0; i < iRows; i++)
            {
                for (int j = 0; j < iColumns; j++)
                {
                    ws.Cells[i + 1, j].Value = kqBaoCao.Rows[i][j];
                }
            }
            excelFile.SaveXls(filename);
        }
        public static void ExportToHtml(DataTable kqBaoCao, string filename)
        {
            StreamWriter oStream = new StreamWriter(filename, false);
            int iRows = kqBaoCao.Rows.Count;
            int iColumns = kqBaoCao.Columns.Count;
            XuatHoaDonThanhToan XuatBaoCaoMatDo = new XuatHoaDonThanhToan();

            oStream.Write("<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>");
            oStream.Write("<div style=' font-family:Arial; display:block; height:134px; padding-left:300; background:url(LOGO_rp.jpg) no-repeat; border-bottom:5px solid #e5e5e5; font-weight:bold; font-size:34px; line-height:140px; margin-bottom:20px;'>HÓA ĐƠN THANH TOÁN</div>");
            oStream.Write("<div style='text-align:center;'><table border=1 style='margin:0 auto; border-style:solid; text-align:left;' align=left cellspacing=0>");
            oStream.Write("<tr height=15 charset='utf-8'>");
            for (int iTenCot = 0; iTenCot < iColumns; iTenCot++)
            {
                oStream.Write("<td width=150 style='background-color:#9eac4a;color:#ffffff; font-size:18px; font-weight:bold; text-align:center;'>");
                oStream.Write(XuatBaoCaoMatDo.dsCotBaoCao[iTenCot].ToString());
                oStream.Write("</td>");
            }
            oStream.Write("</tr>");
            oStream.Write("<tr height=15>");
            for (int i = 0; i < iRows; i++)
            {
                oStream.Write("<tr height=15>");
                for (int j = 0; j < iColumns; j++)
                {
                    oStream.Write("<td width=150>");
                    oStream.Write(kqBaoCao.Rows[i][j].ToString());
                    oStream.Write("</td>");
                }
                oStream.Write("</tr>");
            }
            oStream.Write("</table></div>");
            oStream.Write("</html>");
            oStream.Close();

        }
        public static void TryToDisplayGeneratedFile(string fileName)
        {
            try
            {
                System.Diagnostics.Process.Start(fileName);
            }
            catch (Exception)
            {
                Console.WriteLine(fileName + " created in application folder.");
            }
        }
        public static string MD5Hash(string plainText)
        {
            string cipherText;
            byte[] plainData = new byte[plainText.Length];

            //Chuyen tu string sang mang byte
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            plainData = encoding.GetBytes(plainText);


            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] cipherData = md5.ComputeHash(plainData);

            //Chuyen tu mang byte sang string
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            cipherText = enc.GetString(cipherData);

            return cipherText;
        }
    }
}
